package 网络编程.课后作业;

import com.sun.security.ntlm.Server;
import 集合框架.Set.课堂代码.M1.Person;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

/**
 * 3)、使用TCP网络编程完成用户登录功能：客户端输入用户名和密码，向服务器发出登录请求；服务器接收数据并进行判断，如果用户名和密码均是bjsxt，则登录成功，否则登录失败，返回相应响应信息；客户端接收响应信息并输出登录结果。
 * (1) 用户User类已提供构造方法 public User(String username,String password)。
 * (2) 客户端采用ObjectOutputStream发送封装了用户名和密码的User对象。
 */
public class M3 {

        private static final  long serialVersionUID = 1L;

        private String name;
        private String password;

    public M3(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

/**
 * 客户端类
 */
class UploadClient{
    public static void main(String[] args) throws IOException {
        //提醒接收用户输入
        Scanner input = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String name =input.next();
        System.out.println("请输入密码:");
        String password = input.next();

        //连接服务器利用Socket创建输出流，并将对象输出达到服务器
        Socket socket = new Socket("192.168.1.178",8888);
        OutputStream os = socket.getOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(os);
        oos.writeObject(new M3(name,password));


        //关闭输出流
        socket.shutdownOutput();

        //利用Socket创建输入流，接收服务器验证信息
        InputStream is = socket.getInputStream();
        DataInputStream dis = new DataInputStream(is);
        System.out.println(dis.readUTF());

        //关闭流
        dis.close();
        is.close();
        socket.close();
    }

}
class UploadServer {
    public static void main(String[] args) {

        System.out.println("服务器接收数据！");
        //连接客户端利用Socket创建输出流并接收数据
        ServerSocket ss = null;
        Socket s = null;
        int count = 0; //统计第几个用户登陆
        try {
            //这个必须放在while循环外面，否则会报端口占用得到错误
            ss = new ServerSocket(8888);
            while (true) {
                s = ss.accept();

                new Thread(new ServerThread(s)).start();
                System.out.println("第" + (count++) + "个用户在访问，Ip地址是:" + s.getInetAddress().getHostAddress());
            }


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    static class ServerThread implements Runnable {
        InputStream is = null;
        ObjectInputStream ois = null;
        OutputStream os = null;
        DataOutputStream dos = null;
        Socket s;

        public ServerThread(Socket s) {
            this.s = s;
        }

        @Override
        public void run() {
            try {
                //通过Socket对象拿到输入流
                is = s.getInputStream();
                ois = new ObjectInputStream(is);
                Person p = (Person) ois.readObject();

                //关闭Socket输入流
                s.shutdownOutput();

                //启动Socket输出流，将登陆成功或失败信息返回给客户端
                os = s.getOutputStream();
                dos = new DataOutputStream(os);

                //对接收到的数据进行判定
                if ("lili".equals(p.getName()) && "123123".equals(p.getName())) {
                    dos.writeUTF("登陆成功！");
                } else {
                    dos.writeUTF("登陆失败！");
                }
            } catch
            (IOException | ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                //关流
                try {
                    s.shutdownOutput();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    ois.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }
    }
}



